//----------------------------------------------------------------------------------------------------------------------
// Copyright (c) 2012 James Whitworth
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the \"Software\"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//----------------------------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------------------------
// forward declaration, ensures anything external to this cannot call this function
//----------------------------------------------------------------------------------------------------------------------
local calculate_template_parameter_count = null

//----------------------------------------------------------------------------------------------------------------------
// Describes a function for code generation
//----------------------------------------------------------------------------------------------------------------------
class MockFunction extends CodeGenerator
{
  owner = null

  return_type = null
  parameters = null

  constructor(_name, _flags = 0)
  {
    name = _name
    flags = _flags
    owner = null

    return_type = Parameter()
    parameters = array(0)
  }

  function is_function()
  {
    return true
  }

  function is_templated()
  {
    return false
  }
  
  function is_specialisation()
  {
    return false
  }

  function has_header_data()
  {
    return true
  }

  function has_inline_data()
  {
    return false
  }

  function has_source_data()
  {
    return false
  }

  // Writes a function declaration using this description.
  //
  function write_declaration(stream)
  {
    if (has_flags(CodeGenerator.kConst))
    {
      stream.write("MOCK_CONST_METHOD%d(", parameters.len())
    }
    else
    {
      stream.write("MOCK_METHOD%d(", parameters.len())
    }
    
    // write the name of the function
    //
    assert(typeof(name) == "string")
    assert(name.len() > 0)
    stream.write("%s, ", name)
    
    // Parameter::write_type returns whether an extra space is required
    //
    return_type.write_type(stream)
    write_parameters_declaration(stream)

    stream.write(");")
    stream.new_line()
  }
  
  function write_parameters_declaration(stream)
  {
    // parameters should not be named for a mock so temporarily clear the name when writing
    //
    stream.write("(")
    local argument_count = parameters.len()
    if (argument_count > 0)
    {
      local i = 0
      for (; i != argument_count - 1; ++i)
      {
        local name = parameters[i].name
        parameters[i].name = ""

        parameters[i].write_declaration(stream)
        stream.write(", ")

        parameters[i].name = name
      }

      local name = parameters[i].name
      parameters[i].name = ""

      parameters[i].write_declaration(stream)

      parameters[i].name = name
    }
    stream.write(")")
  }

  function write_definition(stream, file_type)
  {
    // nothing to do
  }
}